<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Auth_model extends CI_Model {

	/**
	 * 添加节点
	 * @param [type] $data [description]
	 */
	public function add_node($data) {
		$rid = $this->db->insert('auth_rule', $data);
		return $rid;
	}

	/**
	 * 获取所有节点列表
	 * @return [type] [description]
	 */
	public function get_nodes() {
		$all_nodes = $this->db->get('auth_rule')->result_array();
		return $all_nodes;
	}

	/**
	 * 获取节点列表
	 * @param  integer $uid [description]
	 * @return [type]       [description]
	 */
	public function uid_nodes($uid = 0) {
        //读取用户所属用户组
        $groups = $this->get_groups($uid);

        $ids = array();
        foreach ($groups as $g) {
            $ids = array_merge($ids, explode(',', trim($g['rules'], ',')));
        }
        $ids = array_unique($ids);
        if (empty($ids)) {
            $_authList[$uid] = array();
            return array();
        }

     	$rules = $this->db->from('auth_rule')
                ->where('status', 0)
                ->where_in('id', $ids)->get()->result_array();
		return $rules;
	}

	public function get_groups($uid =0) {
		static $groups = array();
		$this->db->from('auth_group_access a');
		$this->db->join('auth_group g', 'a.group_id=g.id');
		$this->db->where(array('a.uid'=>$uid, 'g.status'=>0));

		$user_groups = $this->db->get()->result_array();

		$groups[$uid]=$user_groups?$user_groups:array();
        return $groups[$uid];
	}

	/**
	 * 获取单个节点信息
	 * @param  [type] $rid [description]
	 * @return [type]      [description]
	 */
	public function get_node($rid) {
		$data = $this->db->get_where('auth_rule', array('id'=>$rid))->row_array();
		return $data;
	}

	/**
	 * 编辑节点
	 * @param  [type] $rid  [description]
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	public function edit_node($rid, $data) {
		$status = $this->db->update('auth_rule', $data, array('id'=>$rid));
		return $status;
	}

	/**
	 * 添加角色分组
	 * @param [type] $data [description]
	 */
	public function add_role($data) {
		$gid = $this->db->insert('auth_group', $data);
		return $gid;
	}

	/**
	 * 编辑角色分组
	 * @param  [type] $gid [description]
	 * @return [type]      [description]
	 */
	public function edit_role($gid, $data) {
		$status = $this->db->update('auth_group', $data, array('id'=>$gid));
		return $status;

	}

	/**
	 * 调取角色列表
	 * @return [type] [description]
	 */
	public function get_roles() {
		$roles = $this->db->get('auth_group')->result_array();
		return $roles;
	}

	/**
	 * 获取角色详细信息
	 * @param  [type] $gid [description]
	 * @return [type]      [description]
	 */
	public function get_role($gid) {
		$data = $this->db->get_where('auth_group', array('id'=> $gid))->row_array();
		return $data;
	}

	/**
	 * 设置角色权限
	 * @param [type] $gid   [description]
	 * @param [type] $rules [description]
	 */
	public function set_access($gid, $rules) {
		$status = $this->db->update('auth_group',$rules, array('id' => $gid));
		return $status;
	} 

	/**
	 * 获取角色原有权限
	 * @param  [type] $gid [description]
	 * @return [type]      [description]
	 */
	public function get_access($gid) {
		$data = $this->db->select('rules')->get_where('auth_group', array('id' => $gid))->row_array();
		return $data;
	}


}

/* End of file auth_model.php */
/* Location: ./application/models/auth_model.php */